package com.fira.web.controller.edu;

import java.util.List;

import com.fira.common.utils.StringUtils;
import com.fira.edu.domain.EduCourse;
import com.fira.edu.service.IEduCourseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fira.common.annotation.Log;
import com.fira.common.enums.BusinessType;
import com.fira.edu.domain.EduCourseMeal;
import com.fira.edu.service.IEduCourseMealService;
import com.fira.common.core.controller.BaseController;
import com.fira.common.core.domain.AjaxResult;
import com.fira.common.utils.poi.ExcelUtil;
import com.fira.common.core.page.TableDataInfo;

/**
 * 课程套餐Controller
 * 
 * @author tony
 * @date 2021-10-01
 */
@Controller
@RequestMapping("/edu/meal")
public class EduCourseMealController extends BaseController
{
    private String prefix = "edu/meal";

    @Autowired
    private IEduCourseMealService eduCourseMealService;
    @Autowired
    private IEduCourseService eduCourseService;

    @RequiresPermissions("edu:meal:view")
    @GetMapping()
    public String meal()
    {
        return prefix + "/meal";
    }

    /**
     * 查询课程套餐列表
     */
    @RequiresPermissions("edu:meal:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EduCourseMeal eduCourseMeal)
    {
        startPage();
        List<EduCourseMeal> list = eduCourseMealService.selectEduCourseMealList(eduCourseMeal);
        return getDataTable(list);
    }

    /**
     * 导出课程套餐列表
     */
    @RequiresPermissions("edu:meal:export")
    @Log(title = "课程套餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EduCourseMeal eduCourseMeal)
    {
        List<EduCourseMeal> list = eduCourseMealService.selectEduCourseMealList(eduCourseMeal);
        ExcelUtil<EduCourseMeal> util = new ExcelUtil<EduCourseMeal>(EduCourseMeal.class);
        return util.exportExcel(list, "meal");
    }

    /**
     * 新增课程套餐
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap,String courseId)
    {
        if(StringUtils.isNotEmpty(courseId)){
            //查询课程信息
            EduCourse course = eduCourseService.selectEduCourseById(courseId);
            if(course != null){
                modelMap.put("courseName",course.getCourseName()+"-"+course.getCourseType());
                modelMap.put("courseId",courseId);
            }
        }else{
            //查询所有课程
            List<EduCourse> courseList = eduCourseService.selectEduCourseList(null);
            modelMap.put("courseList",courseList);
        }
        return prefix + "/add";
    }

    /**
     * 新增保存课程套餐
     */
    @RequiresPermissions("edu:meal:add")
    @Log(title = "课程套餐", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EduCourseMeal eduCourseMeal)
    {
        return toAjax(eduCourseMealService.insertEduCourseMeal(eduCourseMeal));
    }

    /**
     * 修改课程套餐
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        EduCourseMeal eduCourseMeal = eduCourseMealService.selectEduCourseMealById(id);
        EduCourse course = eduCourseService.selectEduCourseById(eduCourseMeal.getCourseId());
        if(course != null){
            eduCourseMeal.setCourseId(course.getCourseName() + "-" + course.getCourseType());
        }
        mmap.put("eduCourseMeal", eduCourseMeal);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程套餐
     */
    @RequiresPermissions("edu:meal:edit")
    @Log(title = "课程套餐", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EduCourseMeal eduCourseMeal)
    {
        return toAjax(eduCourseMealService.updateEduCourseMeal(eduCourseMeal));
    }

    /**
     * 删除课程套餐
     */
    @RequiresPermissions("edu:meal:remove")
    @Log(title = "课程套餐", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(eduCourseMealService.deleteEduCourseMealByIds(ids));
    }
}
